/***
 * 将目录下的小图合成一张大图集(texture)
 * 设定最大宽高,以单行最高的单张图为单行高度,逐行排序,
 * 生成含坐标的.json索引文件
 */

var images = require("images");
var fs = require('fs');



var paths = [];

var rootDir = "D:\\M2\\Texture\\";
var children = fs.readdirSync(rootDir).filter(function (s) {
    return s.lastIndexOf(".json") > 0;
});
for (var j = 0; j <= children.length; j++) {
    paths.push(rootDir + "/" + children[j]);
}

function replaceContent(paths) {
    for (var i in paths) {
        try {
            var context = fs.readFileSync(paths[i], "UTF-8");
            context = context.replace(RegExp('.wil', 'g'), '')
            fs.writeFileSync(paths[i], context, "UTF-8")
        } catch (e) {
            console.log("[ERR ]  " + JSON.stringify(e));
        }

    }
}

// replaceContent(paths);
function replaceContent(paths) {
    for (var i in paths) {
        try {
            var context = fs.readFileSync(paths[i], "UTF-8");
            var json = JSON.parse(context);
            json.file = json.file+".png"
            fs.writeFileSync(paths[i], JSON.stringify(json), "UTF-8")
        } catch (e) {
            console.log("[ERR ]  " + JSON.stringify(e));
        }

    }
}
replaceContent(paths);
console.log("  success!!! ");